<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>hcreate</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_005_899">&nbsp;</a>NAME</h4><blockquote>
hcreate, hdestroy, hsearch - manage hash search table
</blockquote><h4><a name = "tag_000_005_900">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="search.h.html">search.h</a>&gt;

int hcreate(size_t <i>nel</i>);
void hdestroy(void);
ENTRY *hsearch (ENTRY <i>item</i>, ACTION <i>action</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_005_901">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>hcreate()</i>,
<i>hdestroy()</i>
and
<i>hsearch()</i>
functions manage hash search tables.
<p>
The
<i>hcreate()</i>
function allocates sufficient space for the table, and must be called before
<i>hsearch()</i>
is used.  The
<i>nel</i>
argument is an estimate of the maximum number of entries that
the table will contain.
This number may be adjusted upward by the
algorithm in order to obtain certain mathematically favourable
circumstances.
<p>
The
<i>hdestroy()</i>
function disposes of the search table, and may be followed by another call to
<i>hcreate()</i>.
After the call to
<i>hdestroy()</i>,
the data can no longer be considered accessible.
<p>
The
<i>hsearch()</i>
function is a hash-table search routine.  It returns a pointer into a hash
table indicating the location at which an entry can be found.  The
<i>item</i>
argument is a structure of type
<b>ENTRY</b>
(defined in the
<i><a href="search.h.html">&lt;search.h&gt;</a></i>
header) containing two pointers:
<i>item.key</i>
points to the comparison key (a
<b>char *</b>),
and
<i>item.data</i>
(a
<b>void *</b>)
points to any other data to be associated with that key.
The comparison function used by
<i>hsearch()</i>
is
<i><a href="strcmp.html">strcmp()</a></i>.
The
<i>action</i>
argument is a member of an enumeration type
<b>ACTION</b>
indicating the disposition of the entry if it cannot be
found in the table.
ENTER indicates that the item should be inserted in the table at an
appropriate point.
FIND indicates that no entry should be made.
Unsuccessful resolution is indicated by the return of a null pointer.
<p>
The hcreate() and hsearch() functions need not be reentrant.
</blockquote><h4><a name = "tag_000_005_902">&nbsp;</a>RETURN VALUE</h4><blockquote>
The
<i>hcreate()</i>
function returns 0 if it cannot allocate sufficient space for the
table, and returns non-zero otherwise.
<p>
The
<i>hdestroy()</i>
function returns no value.
<p>
The
<i>hsearch()</i>
function returns a null pointer if either the action is FIND
and the item could not be found or the action is
ENTER and the table is full.
</blockquote><h4><a name = "tag_000_005_903">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>hcreate()</i>
and
<i>hsearch()</i>
functions may fail if:
<dl compact>

<dt>[ENOMEM]<dd>
Insufficient storage space is available.

</dl>
</blockquote><h4><a name = "tag_000_005_904">&nbsp;</a>EXAMPLES</h4><blockquote>
The following example will read in strings followed by two
numbers and store them in a hash table, discarding duplicates.
It will then read in strings and find the matching entry
in the hash table and print it out.
<pre>
<code>
#include &lt;stdio.h&gt;
#include &lt;search.h&gt;
#include &lt;string.h&gt;

struct info {        /* this is the info stored in the table */
    int age, room;   /* other than the key. */
};

#define NUM_EMPL    5000    /* # of elements in search table */

int main(void)
{
    char string_space[NUM_EMPL*20];    /* space to store strings */
    struct info info_space[NUM_EMPL];  /* space to store employee info*/
    char *str_ptr = string_space;      /* next space in string_space */
    struct info *info_ptr = info_space;/* next space in info_space */
    ENTRY item;
    ENTRY *found_item;    /* name to look for in table */
    char name_to_find[30];

    int i = 0;

    /* create table; no error checking is performed */
    (void) hcreate(NUM_EMPL);
    while (scanf("%s%d%d", str_ptr, &amp;info_ptr-&gt;age,
           &amp;info_ptr-&gt;room) != EOF &amp;&amp; i++ &lt; NUM_EMPL) {

        /* put information in structure, and structure in item */
        item.key = str_ptr;
        item.data = info_ptr;
        str_ptr += strlen(str_ptr) + 1;
        info_ptr++;

        /* put item into table */
        (void) hsearch(item, ENTER);
    }

    /* access table */
    item.key = name_to_find;
    while (scanf("%s", item.key) != EOF) {
        if ((found_item = hsearch(item, FIND)) != NULL) {

            /* if item is in the table */
            (void)printf("found %s, age = %d, room = %d\n",
                found_item-&gt;key,
                ((struct info *)found_item-&gt;data)-&gt;age,
                ((struct info *)found_item-&gt;data)-&gt;room);
        } else
            (void)printf("no such employee %s\n", name_to_find);
    }
    return 0;
}
</code>
</pre>
</blockquote><h4><a name = "tag_000_005_905">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The
<i>hcreate()</i>
and
<i>hsearch()</i>
functions may use
<i><a href="malloc.html">malloc()</a></i>
to allocate space.
</blockquote><h4><a name = "tag_000_005_906">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_005_907">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="bsearch.html">bsearch()</a></i>,
<i><a href="lsearch.html">lsearch()</a></i>,
<i><a href="malloc.html">malloc()</a></i>,
<i><a href="strcmp.html">strcmp()</a></i>,
<i><a href="tsearch.html">tsearch()</a></i>,
<i><a href="search.h.html">&lt;search.h&gt;</a></i>.
<br>
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 1 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

